import { Component, OnInit } from '@angular/core';
import { FormlyFieldConfig, FormlyFormOptions } from '@ngx-formly/core';
import { ApiService } from '@core/services/http/api.service';
import { SubjectService } from '@core/services/events/subject.service';
import { ApesMessageService } from '@shared/apes/message';

@Component({
  selector: 'storeCenter-control',
  templateUrl: './storeCenter-control.component.html',
  styleUrls: ['./storeCenter-control.component.scss'],
})
export class StoreCenterControlComponent implements OnInit {

  // 门店中心总控
  toDoConfig: any = {}; // 部门枚举

  storeModel = {};
  ctx: any = {};
  options: FormlyFormOptions = {};
  subjects = {};
  storeSearchField: FormlyFieldConfig[] = [
    {
      key: 'DEPT_ID_O',
      type: 'conditionInput',
      templateOptions: {
        config: {
          api: 'cm.dept.findAllOperate',
          downFormatter: '\'[\' + value.id + \']\' + value.name',
          init: true,
          operators: ['IN'],
          searchFormId: 'cm.dept.findAllServicePlan',
          type: 'select',
          value: '',
          valueFormatter: '\'[\' + value.id + \']\' + value.name',
          required: true,
        },
        divCol: 6,
        lblCol: 7,
        domain: '{"filters":[{"field":"id","operator":"LIKE","value":"{{value}}"},{"field":"name","operator":"LIKE","value":"{{value}}"}],"groups":[],"operator":"or"}',
        label: '所属门店',
        placeholder: '请输入所属门店编码或名称',
      },
    },
  ];
  storeShowField: FormlyFieldConfig[] = [
    {
      key: 'LABEL_ONE',
      type: 'conditionInput',
      templateOptions: {
        divCol: 24,
        inputCol: 22,
        label: '车辆位置',
        lblCol: 2,
        config: {
          items: '[]',
          labelListCol: '2',
          operators: ['EQ'],
          type: 'label-list',
          value: '',
        },
      },
    },
    {
      key: 'LABEL_TWO',
      type: 'conditionInput',
      templateOptions: {
        divCol: 24,
        inputCol: 22,
        label: '服务状态',
        lblCol: 2,
        config: {
          items: '[]',
          labelListCol: '2',
          operators: ['EQ'],
          type: 'label-list',
          value: '',
        },
      },
    },
    // {
    //   key: 'CREDITS',
    //   templateOptions: {
    //     col : '24',
    //     label: '信用额度',
    //     divCol: 24,
    //     lblCol: 2,
    //     value: '后续开发,敬请期待',
    //   },
    //   type: 'important-note',
    // },
  ];
  searchLoading: boolean = false;
  creditsData: any[] = [
    // {
    //   'totalBalanceLine': 80000,
    //   'partnerName': '广州赛壹汽车服务有限公司',
    //   'totalIncurred': 20000,
    //   'partnerId': 'K6004',
    // },
  ];

  config: any = [
    {
      title: '其他',
      api: 'fn.controlWaitDisposalService.findAll',
      resData: {},
      drawer: {
        'title': 'data.LEFTTEXT',
        'action': 'drawer',
        'formId': 'data.formId',
        'width': '40vw',
      },
    },
  ];

  constructor(
    private apiService: ApiService,
    protected subject: SubjectService,
    private msg: ApesMessageService,
  ) {

  }

  ngOnInit() {
    // this.ctx.subjects = this.subjects;
    this.init();
  }

  init() {
    this.ctx.model = this.storeModel;
    this.ctx.subjects = this.subjects;
    this.options.formState = this.ctx;
  }

  queryData() {
    if (!this.checkSendData()) {
      return;
    }
    this.changDeptId();
    this.searchLoading = true;
    this.obtainMainList();
    this.obtainCreditsData();
  }

  changDeptId() {
    this.toDoConfig['dept_id'] = this.storeModel['DEPT_ID_O'];
    this.config.forEach((con) => {
      con.resData = {
        'dept_id': this.storeModel['DEPT_ID_O'],
      };
    });
  }

  checkSendData() {
    let check = false;
    if (this.storeModel.hasOwnProperty('DEPT_ID_O') && this.storeModel['DEPT_ID_O'] != null) {
      check = true;
    } else {
      this.msg.error('请选择所属门店！');
    }
    return check;
  }

  obtainMainList() {
    let api = 'fn.control.obtainMainList';
    let dept_id = { 'field': 'DEPT_ID_O', 'operator': 'IN', 'value': this.storeModel['DEPT_ID_O'] };
    let sendData = {
      condition: [dept_id],
      customAction: false,
      customQuery: true,
      noSum: true,
      page: 0,
      size: 50,
    };
    this.apiService.post(api, { 'body': JSON.stringify(sendData) })
      .subscribe((body) => {
        if (body.hasOwnProperty('labelList')) {
          this.subject.publish(this.ctx, 'labelListData', body.labelList);
        }
        this.searchLoading = false;
      });
  }

  obtainCreditsData() {
    let api = 'acc.creditLine.findCompanyCreditLine';
    let sendData = {
      dept: { id: this.storeModel['DEPT_ID_O'] },
    };
    this.apiService.post(api, { 'body': JSON.stringify(sendData) })
      .subscribe((body) => {
        console.log('请求结果', body);
        if (body) {
          this.creditsData = body;
        }
      });
  }


}

